package hot100;

import java.util.Arrays;

public class e4_除自身以外数组的乘积 {
    public int[] productExceptSelf(int[] nums) {
        int len = nums.length;
        int[] res = new int[len];
        if(len == 0) return res;
        Arrays.fill(res, 1);
        int prefix = 1;
        for (int i = 0; i < len; i++) {
            res[i] = prefix;
            prefix = prefix * nums[i];
        }
        int postfix = 1;
        for (int i = len-1; i >= 0 ; --i) {
            res[i] = postfix * res[i];
            postfix = postfix * nums[i];
        }
        return res;
    }
}
